package org.tnaggs.project.manager;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.tnaggs.project.constants.Roles;
import org.tnaggs.project.model.Contact;
import org.tnaggs.project.model.Player;
import org.tnaggs.project.model.Team;
import org.tnaggs.project.repository.ContactRepository;
import org.tnaggs.project.repository.ConversionRepository;
import org.tnaggs.project.repository.PlayerRepository;
import org.tnaggs.project.repository.TeamRepository;

public class TeamManager {

	private PlayerRepository playerRepo;
	private TeamRepository teamRepo;
	
	@Autowired
	private ContactRepository contactRepo;
	
	@Autowired
	private ConversionRepository conversionRepo;
	
	
	public TeamManager(TeamRepository teamRepo, PlayerRepository playerR) {
		this.setPlayerRepo(playerR);
		setTeamRepo(teamRepo);
	}

	public void createTeam(Team team) {
		teamRepo.insert(team);
	}

	public TeamRepository getTeamRepo() {
		return teamRepo;
	}

	public void setTeamRepo(TeamRepository teamRepo) {
		this.teamRepo = teamRepo;
	}
	
	public Team getTeam(int teamID) {
		return teamRepo.findByID(teamID);
	}
	
	public List<Player> getPlayers(long teamID) {
		return playerRepo.findAllByTeamID(teamID);
	}
	
	public List<Player> getPlayers(String teamName) {
		Team team = teamRepo.findByName(teamName);
		return playerRepo.findAllByTeamID(team.getTeamID());
	}
	
	public List<Contact> getAllCoaches() {
		ArrayList<Contact> results = new ArrayList<Contact>();
		List<Contact> contacts = contactRepo.findAll();
		for(Contact contact : contacts) {
			if(conversionRepo.getRole(contact.getRoleID()).equalsIgnoreCase(Roles.COACH)) {
				results.add(contact);
			}
		}
        return results;
	}

	public ContactRepository getContactRepo() {
		return contactRepo;
	}

	public void setContactRepo(ContactRepository contactRepo) {
		this.contactRepo = contactRepo;
	}

	public PlayerRepository getPlayerRepo() {
		return playerRepo;
	}

	public void setPlayerRepo(PlayerRepository playerRepo) {
		this.playerRepo = playerRepo;
	}

	public Contact getCoach(int contactID) {
		return contactRepo.findByID(contactID);
	}
	
	
	
	
}
